package com.czk.boot.spark.controller.sql;

import com.czk.boot.spark.pojo.Person;
import org.apache.spark.sql.Dataset;
import org.apache.spark.sql.Encoders;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/12/16 10:27
 */
@RestController
@RequestMapping("/spark")
public class SparkSqlController {
    @Autowired
    private SparkSession sparkSession;

    @GetMapping("/sql")
    public List<Person> listPerson() throws InterruptedException {
        Dataset<Row> df = sparkSession.read()
                .format("jdbc")
                .option("url", "jdbc:mysql://localhost:3306/test")
                .option("driver", "com.mysql.jdbc.Driver")
                .option("user", "root")
                .option("password", "1234")
                .option("dbtable", "user")
                .load();
        // 转为ds
        df.show();
        Dataset<Person> ds = df.as(Encoders.bean(Person.class));
        Person[] collect =(Person[]) ds.collect();
        List<Person> list = Arrays.asList(collect);
        return list;
    }
}
